<?php
/*
----------------------------------------
-----    Page logic description    -----
----------------------------------------
REFERENCES FROM:
    * cp__tm.php (as include)
    * dashboard.php (as ajax)
    * quick_actions.php (as include)
INPUT:
    m       => monitor object (if page is included)
    
    maction => ("save", "new", "resume_suspend")
    mid     => monitor id which should be processed
    mid_ref => reference for new monitor (optional)
    
    out     => ["full" (default), "" (none), "row", "det"]

    do_expand_monitor

OUTPUT: 
	Page
----------------------------------------
*/
if (!isset($r)) $r = "../";
require_once($r."_engine/header_nocache.php");
require_once($r."_engine/c_account.php");
require_once($r."_engine/c_monitor.php");
require_once($r."_engine/c_resources_storage.php");
require_once($r."_engine/functions.php");
require_once($r."_engine/mail_functions.php");
require_once($r."_header_ext.php");




if (!isset($account)) {
    $account = GetActiveAccount();
}
if (!isset($categories)) {
    $categories = new CCategories($account);
}


include "cp__mon_process.php";


//-----------------------------------------------------------------------------

$curr_mon = $mon_cntr;
$mon_cntr++;
//----------------------------------------------------------------------------


if (!isset($do_expand_monitor)) {
    $do_expand_monitor = false;

    if (@$_REQUEST["do_expand_monitor"]) {
        $do_expand_monitor = true;
    }
}
                             
if (@$_REQUEST["maction"] == "new") {
    $m = new CMonitor();
    $m->InitNew($account);
    $m->id = 0;

    if (@$_REQUEST["mid_ref"])
    {
        $monitor_ref = new CMonitor();
    	$monitor_ref->LoadById($_REQUEST["mid_ref"]) or die("Incorrect data: mid_ref.");
   	    
    	$account->CheckOwner($monitor_ref);
    	$m->InitNewFromAnother($monitor_ref);
    }
    
    $do_expand_monitor = true;
}

if (@$_REQUEST["mid"]) {
    $m = new CMonitor();
	$m->LoadByQuery( $q = '
SELECT 
    monitors.*, categories.name AS category_name,
    resources_storage.download_dt AS rs_download_dt,
    resources_storage.resource_status AS rs_resource_status
FROM monitors 
LEFT JOIN categories ON (monitors.category_id = categories.id) 
LEFT JOIN resources_storage_max_id ON (monitors.id = resources_storage_max_id.monitor_id)
LEFT JOIN resources_storage ON (resources_storage_max_id.max_id = resources_storage.id)
WHERE monitors.id = '.$_REQUEST["mid"]
    );
}

if (!isset($m)) {
    var_dump($_REQUEST);
    die("Access denied: monitor is not loaded."); // TODO: make as a function
}

if (!isset($out)) {
    $out = "full";
}
if ($out == "det") {
    $do_expand_monitor = "true";
}


/* ----------------------------------------------------- */
/* -----                   OUTPUT                  ----- */
/* ----------------------------------------------------- */
@define('NL',  "\n");

if ($out == "full")
echo '<a name=aMon_'.$m->id.'></a><div id=divMon_'.$m->id.'>'.NL;

if ($out == "full" || $out == "row")
{
echo '<div id=divMonRow_'.$m->id.'>'.NL;
include "cp__mon_row.php";
echo '</div>'.NL;
}

$tr_form_ext = $do_expand_monitor ? 
    '' :
    'style="display: none;"' ;


if ($out == "full")
echo '
<table class=dash-monitor id=idMonDiv_'.$m->id.' '.$tr_form_ext.'>
<tr><td style="padding-left: 20;">
<div id=idMonDivContent_'.$m->id.'>
';

if ($out == "full" || $out == "det")
{
    if ($do_expand_monitor) {
        $mid = ($m->id <= 0 ? 0 : $m->id);
        include "cp__mon_det.php";
    }
}

if ($out == "full")
echo '
</div>
</td></tr>
</table>
';

if ($out == "full") 
echo '</div>';


?>
